package com.wica.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wica.facetube.R;
import com.wica.view.tab_preference.TabPreferencesSearch;


public class DialogActivity extends Activity implements OnKeyListener{
	
	// *************** Constants ************** //  
	// Log constants
    protected static final String LOG_TAG = "PTv.DialogActivity";
    
    // *************** Attributes ************************* //
	private Button search,exit;
	private EditText searchStringEditText;
	private OnClickListener searchListener;
	private static final int SETTINGS = Menu.FIRST+1;
	private static final int SET_MOOD = Menu.FIRST+2;
	private static final int SET_TEXTLOCATION = Menu.FIRST+3;
	// Request code for ActivityForResult
	private static final int REQUESTCODE_MOOD = 1;
	private static final int REQUESTCODE_TEXTLOCATION = 2;
	private static final int REQUESTCODE_SETTINGS = 3;

	// *************** Methods **************************** //
    @Override
	protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);        

        // you need to call this prior to "Window.setFeatureDrawableResource" to personalize the icon on the left side
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        
        // Inflate our UI from its XML layout description.
        setContentView(R.layout.searchbar);

        // Search icon on the left side
        getWindow().setFeatureDrawableResource(	Window.FEATURE_LEFT_ICON,
        										R.drawable.search);

        // Get display items for later interaction
        search = (Button) findViewById(R.id.btn_start_search);
        exit = (Button) findViewById(R.id.btn_exit);
        searchStringEditText = (EditText) findViewById(R.id.txt_query_prefill);
        
        // This allows us to later extend the text buffer.
//        searchStringEditText.setText(searchStringEditText.getText(), TextView.BufferType.EDITABLE);
        searchStringEditText.setOnKeyListener(new OnKeyListener(){

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER){		
					searchListener.onClick(search);			
					return true;
		  	  	}else {
		  	  		return onKeyDown(keyCode, event);
		  	  	}
			}
        	
        });
        Intent intent = new Intent(DialogActivity.this, ELSearch.class);
        intent.putExtra("searchString", "");
    	setResult(RESULT_OK,intent);

        // Attach actions to buttons
    	searchListener = new OnClickListener() {
            public void onClick(View v) {
            	// return data from EditText
                Intent intent = new Intent(DialogActivity.this, ELSearch.class);
                intent.putExtra("searchString", searchStringEditText.getText().toString());
            	setResult(RESULT_OK,intent);
            	Log.v(LOG_TAG,LOG_TAG+"devolvemos el control");
            	finish();
            }
        };
        search.setOnClickListener(searchListener);         
        exit.setOnClickListener(
        		new OnClickListener() {
                    public void onClick(View v) {
                    	finish();
                    }
                });

    }
    
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_ENTER){		
			searchListener.onClick(search);			
			return true;
  	  	}else {
  	  		return super.onKeyDown(keyCode, event);
  	  	}
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle all of the possible menu actions.
        switch (item.getItemId()) {
        case SETTINGS:
        	this.startActivityForResult(new Intent(this, TabPreferencesSearch.class), REQUESTCODE_SETTINGS);        	
        	break;
        case SET_MOOD:
        	this.startActivityForResult(new Intent(this, MoodDialog.class), REQUESTCODE_MOOD);
            break;
    	case SET_TEXTLOCATION:
    		this.startActivityForResult(new Intent(this, TextLocationDialog.class), REQUESTCODE_TEXTLOCATION);
    		break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // Build the menu shown when we hit menu button (KeyEvent class - KEYCODE_MENU) while playing the video.
        menu.add(0, SET_MOOD, 1, R.string.set_mood)
				.setShortcut('0', 'r')
				.setIcon(R.drawable.stat_happy);
        menu.add(0, SET_TEXTLOCATION, 2, R.string.set_textLocation)
        		.setShortcut('0', 'r')
        		.setIcon(R.drawable.menu_location_by_text);
        menu.add(0, SETTINGS, 3, R.string.settings)
        		.setShortcut('0', 'r')
        		.setIcon(R.drawable.ic_settings);
        return true;
    }    
    
}
